#include "gui_background.h"

SDL_Surface* gui_background_get_background(main_obj *mobj) {
    return NULL;
}

back_settings* gui_background_init_background(main_obj *mobj) {
    
    SDL_Surface *b = IMG_Load("res/main/checker.png"); //gui_util_load_png_img("res/main/checker.png");
    SDL_Surface *s = SDL_CreateRGBSurface(SDL_HWSURFACE, mobj->s->vs->screen_width + (b->w * 5), mobj->s->vs->screen_height + (b->h * 5), mobj->s->vs->screen_bbp, 0, 0, 0, 0);
    if (b == NULL) {
        printf("Impossibile caricare checker.png => %s\n", IMG_GetError());
        exit(1);
    }
    int di = (mobj->s->vs->screen_width / b->w) + 5;
    int dj = (mobj->s->vs->screen_height / b->h) + 5;
    int i = 0, j = 0;
    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = b->w;
    rect.h = b->h;
    for (i = 0; i < di; i++) {
        for (j = 0; j < dj; j++) {
            rect.x = i * b->w;
            rect.y = j * b->h;
            SDL_BlitSurface(b, 0, s, &rect);
        }
    }
    back_settings *bs = g_malloc0(sizeof (back_settings));
    mobj->main_background = s;
    bs->background = s;
    bs->speedx = 0.2;
    bs->speedy = 0.2;
    bs->tile_w = b->w;
    bs->tile_h = b->h;
    bs->mill_delay = 0;
    bs->mill_elapsed = 0;
    bs->posx = 0;
    bs->posy = -(b->h);
    return bs;
}

void gui_background_update(main_obj *mobj) {
    mobj->bs->mill_elapsed += mobj->tu->time_updated - mobj->tu->prev_time_updated;
    if (mobj->bs->mill_elapsed > mobj->bs->mill_delay) {
        mobj->bs->mill_elapsed = 0;
        if (mobj->bs->posx < -(mobj->bs->tile_w)) {
            mobj->bs->posx = 0;
            mobj->bs->posy = -(mobj->bs->tile_h + 10);
        } else {
            mobj->bs->posx -= mobj->bs->speedx;
            mobj->bs->posy += mobj->bs->speedy;
        }
    }
}